﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Nhanvien
{
    public partial class popSuataikhoan : Form
    {
        private Int16 nvid = -1;
        public popSuataikhoan(Int16 id)
        {
            InitializeComponent();
            nvid = id;
        }

        private void popSuataikhoan_Load(object sender, EventArgs e)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;

            ls = new trangthai();
            ls.name = "Hoạt động";
            ls.value = "1";
            list.Add(ls);

            ls = new trangthai();
            ls.name = "Không hoạt động";
            ls.value = "0";
            list.Add(ls);

            cbtrangthai.DataSource = list;
            cbtrangthai.DisplayMember = "name";
            cbtrangthai.ValueMember = "value";

            adminNhanvien adnv = new adminNhanvien();
            DataTable dtnv = adnv.chitiet(nvid);
            if (dtnv != null && dtnv.Rows.Count > 0)
            {
                txttennv.Text = dtnv.Rows[0]["ten"].ToString();
                txttaikhoan.Text = dtnv.Rows[0]["username"].ToString();
                cbtrangthai.Text = (dtnv.Rows[0]["trangthai"].ToString() == "True") ? "Hoạt động" : "Không hoạt động";
            }
        }
        public delegate void SuaNV(int outid);
        // khai báo 1 kiểu hàm delegate
        public SuaNV MyFunction;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {// tại đây gọi nó
                string ten = txttennv.Text.Trim();
                string vitri = "";
                string username = txttaikhoan.Text.Trim();
                string password = Querys.Md5(txtmatkhau.Text.Trim());
                Int16 trangthai = Int16.Parse((cbtrangthai.SelectedValue == null) ? "0" : cbtrangthai.SelectedValue.ToString());
                adminNhanvien vn = new adminNhanvien();
                int outid = vn.sua(nvid, ten, vitri, username, password, trangthai);
                MyFunction(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
